<?php
/* Captcha 
	Class by infoxp
*/
session_start();
	
	class Captcha {
		private $lenCode=5;
		
		const INVALID_CODE = 0;
		const VALID_CODE = 1;
		
		public function __construct($lenCode=5){	
			$this->setLenCode($lenCode);
				
				if(!isset($_SESSION["captcha_content"]))
				$_SESSION["captcha_content"] = 0;
				
		}
		
		public function generate(){
			header("Content_type: image/png");
			$width = $this->LenCode()*25;
			$height = 35;
			
			$img = imagecreate($width,$height);
			//imagecreatefrompng("../design/Basic/images/captcha/fond.png");
			
				 imagecolorallocate($img,200,200,200);
				
					
					$alphabet = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9");
					
						$save = '';
						 
 						//getenv('GDFONTPATH=' . realpath('.'));

						for($i=0;$i<$this->LenCode();$i++)
						{	
							$r = mt_rand(0,100);
							$g = mt_rand(0,100);
							$b = mt_rand(0,100);
							$rand = mt_rand(0,count($alphabet)-1);
							$code = $alphabet[$rand];
							$save .= $code;
							
							
								$colorLettre = imagecolorallocate($img,$r,$g,$b);
								$font = mt_rand(15,24);
								$hfontsize = imagefontheight($font);
								
								$espacementLettre = 0.8;
								
								$fontfile = array("../fonts/arial.ttf","../fonts/bloody.ttf");
								
							imagefttext($img,$font,mt_rand(0,30),10+($height * (($i*$espacementLettre))-$this->LenCode()*$i),$height/2*1.5,$colorLettre,$fontfile[mt_rand(0,count($fontfile)-1)],$code);
							
							
						}
						
						for($i=0;$i<=mt_rand(1,10);$i++){
							$colorline = imagecolorallocate($img,0,0,0);
							imageline($img,mt_rand(0,$width),mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$height),$colorline);
						}
						
						
			
						$_SESSION["captcha_content"] = $save;
						
						$_SESSION["captcha_lib"] = serialize($this);
						imagepng($img);
					
		}
		
		public function checking($result){
			if(!isset($_SESSION["captcha_content"]) || $_SESSION["captcha_content"] != $result)
				return self::INVALID_CODE;
				
				return self::VALID_CODE;
		}
		
	
		private function LenCode(){
			return $this->lenCode;	
		}
		
		public function setLenCode($lenCode){
			$this->lenCode = $lenCode;
			return $this;	
		}
		
	}

?>